package com.hadwinling.lot.service;

import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;

import org.apache.iotdb.session.SessionDataSet;
import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
import org.apache.iotdb.tsfile.write.record.Tablet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * @description:
 * @author: hadwinling
 * @time: 2021/2/5 下午3:24
 */
@Service
public class LotdbService {

    @Autowired
    private Session lotSession;


    /**
     * 设置存储组
     * @param storageGroupId
     * @throws StatementExecutionException
     * @throws IoTDBConnectionException
     */
    public void setStorageGroup(String storageGroupId) throws StatementExecutionException, IoTDBConnectionException {
        lotSession.setStorageGroup(storageGroupId);
    }


    /**
     * 创建单个时间序列
     * @param path
     * @param dataType
     * @param encoding
     * @param compressor
     * @param props
     * @param tags
     * @param attributes
     * @param measurementAlias
     */
    public void createTimeseries(String path, TSDataType dataType,
                                 TSEncoding encoding, CompressionType compressor, Map<String, String> props,
                                 Map<String, String> tags, Map<String, String> attributes, String measurementAlias){
        try {
            lotSession.createTimeseries(path, dataType, encoding, compressor, props, tags, attributes, measurementAlias);
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建多个时间序列
     * @param paths
     * @param dataTypes
     * @param encodings
     * @param compressors
     * @param propsList
     * @param tagsList
     * @param attributesList
     * @param measurementAliasList
     */
    public void createMultiTimeseries(List<String> paths, List<TSDataType> dataTypes,
                               List<TSEncoding> encodings, List<CompressionType> compressors,
                               List<Map<String, String>> propsList, List<Map<String, String>> tagsList,
                               List<Map<String, String>> attributesList, List<String> measurementAliasList){
        try {
            lotSession.createMultiTimeseries(paths, dataTypes, encodings, compressors, propsList, tagsList, attributesList, measurementAliasList);
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询
     * @return
     * @throws StatementExecutionException
     * @throws IoTDBConnectionException
     */
    public SessionDataSet searchAll() throws StatementExecutionException, IoTDBConnectionException {
        SessionDataSet sessionDataSet = lotSession.executeQueryStatement("select * from root.ln ");
        return sessionDataSet;
    }

    /***
     * 写入一行设备
     * @param deviceId
     * @param time
     * @param measurements
     * @param types
     * @param values
     */
    public String insertRecord(String deviceId, long time, List<String> measurements, List<TSDataType> types, List<Object> values){
        try {
            lotSession.insertRecord(deviceId, time, measurements, types, values);
            return "over";
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
            return "false";
        } catch (StatementExecutionException e) {
            e.printStackTrace();
            return "false";
        }
    }

    /**
     * 写入多行设备
     * @param deviceIds 要写入到的设备好
     * @param times     时间戳
     * @param measurementsList
     * @param typesList
     * @param valuesList
     * @return
     */
    public String insertRecords(List<String> deviceIds, List<Long> times, List<List<String>> measurementsList, List<List<TSDataType>> typesList, List<List<Object>> valuesList){
        try {
            lotSession.insertRecords(deviceIds, times, measurementsList, typesList, valuesList);
            return "over";
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
            return "false";
        } catch (StatementExecutionException e) {
            e.printStackTrace();
            return "false";
        }
    }

    /**
     * 写入一个设备数据块
     * @param tablet
     * @return
     */
    public String insertTablet(Tablet tablet){
        try {
            lotSession.insertTablet(tablet);
            return "over";
        } catch (StatementExecutionException e) {
            e.printStackTrace();
            return "false";
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
            return "false";
        }
    }

    /**
     * 写入多个设备数据块
     * @param tables
     * @return
     */
    public String insertTablets(Map<String,Tablet> tables){
        try {
            lotSession.insertTablets(tables);
            return "over";
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
            return "false";
        } catch (StatementExecutionException e) {
            e.printStackTrace();
            return "false";
        }
    }

    /**
     * 生成时间序列
     * @param path 路径
     * @param dataType  数据类型
     * @param encoding  编码类型
     * @param compressor    压缩类型。
     * @return
     */
    public String creatTimeseries(String path, TSDataType dataType, TSEncoding encoding, CompressionType compressor){
        try {
            lotSession.createTimeseries(path, dataType, encoding, compressor);
            return "over";
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
            return "false";
        } catch (StatementExecutionException e) {
            e.printStackTrace();
            return "false";
        }
    }


    /**
     * 根据设备的id 进行查询数据
     * @param deviceId
     * @return
     */
    public Object queryAllByDeviceId(String deviceId){
        try {
            SessionDataSet sessionDataSet = lotSession.executeQueryStatement("select * from " + deviceId + "\"");
            return sessionDataSet;
        } catch (StatementExecutionException e) {
            e.printStackTrace();
            return "null";
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
            return null;
        }
    }





}
